// utils/websocket/handlers/private.js

import { MESSAGE_TYPES } from '../messageTypes';

/**
 * 处理私聊消息
 */
export default function handlePrivateMessage(message) {
  console.log('收到私聊消息:', message);

  const content = message.message;
  const userIdMatch = /用户id(\d+)/.exec(content);

  const userId = userIdMatch ? parseInt(userIdMatch[1], 10) : null;

  if (!userId) {
    uni.showToast({ title: '消息格式错误', icon: 'none' });
    return;
  }

  // 广播给页面监听
  uni.$emit('websocket-private', {
    type: MESSAGE_TYPES.PRIVATE,
    userId,
    raw: message
  });

  uni.showToast({
    title: `用户 ${userId} 发来了私信`,
    icon: 'none',
    duration: 3000
  });
}